package com.lancoo.ccas53.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.Campus;
import com.lancoo.ccas53.mapper.CampusMapper;
import com.lancoo.ccas53.service.CampusService;
import com.lancoo.ccas53.util.CollComparatorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 校区及设置 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Service
public class CampusServiceImpl extends ServiceImpl<CampusMapper, Campus> implements CampusService {


    @Resource
    private CollComparatorUtil collComparatorUtil;


    @Override
    public List<Campus> syncCampus(Integer taskId, List<String> campusNameList) {

        List<Campus> campusList = new ArrayList<>();

        campusNameList.forEach(campusName -> {
            Campus campus = new Campus();
            campus.setTaskId(taskId);
            campus.setCampusName(campusName);
            campusList.add(campus);
        });

        List<Campus> oldCampusList = this.lambdaQuery().eq(Campus::getTaskId, taskId).list();
        List<Campus> addCampus = collComparatorUtil.getAdded(oldCampusList, campusList);

        if (CollUtil.isNotEmpty(addCampus)) {
            this.saveBatch(addCampus);
        }
        oldCampusList.addAll(addCampus);
        return oldCampusList;
    }

}
